package com.xiaodu.mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaodu.mybatis.entity.UserEntity;
import com.xiaodu.mybatis.mapper.UserMapper;
import com.xiaodu.mybatis.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: dutianjie
 * @date: 2021/4/16
 * @version: v1.0.0
 * @description: 描述
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserEntity oneUser(String id) throws Exception {
        userMapper.selectById(id);
        userMapper.selectOne(id);
        return userMapper.getOne(id);
    }

    //todo
    @Override
    public List<UserEntity> listUser(List<String> ids) throws Exception {
        return userMapper.listUser(ids);
    }
    //todo
    @Override
    public List<UserEntity> listUser(UserEntity userEntity) throws Exception {
        return null;
    }
    //todo
    @Override
    public UserEntity addUser(UserEntity userEntity) throws Exception {
        userEntity.setuId(UUID.randomUUID().toString().replaceAll("-",""));
//        userEntity.setCreateTime(new Date());
        userMapper.insertUser(userEntity);
        return userEntity;
    }
    //todo
    @Override
    public int batchUser(List<UserEntity> users) throws Exception {
        return 0;
    }
    //todo
    @Override
    public int delUser(String id) throws Exception {
        return userMapper.deleteUser(id);
    }
    //todo
    @Override
    public int updateUser(UserEntity userEntity) throws Exception {
        return userMapper.updateUser(userEntity);
    }

    @Override
    public List<UserEntity> pageUser(Integer pageNo, Integer pageSize) {
        pageNo = pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize <= 0 ? 5 : pageSize;
        RowBounds rowBounds = new RowBounds((pageNo -1) * pageSize, pageSize);
        return userMapper.selectAll(rowBounds);
    }

    public PageInfo<UserEntity> pageHelperUser(Integer pageNo, Integer pageSize) {
        pageNo = pageNo <= 0 ? 1 : pageNo;
        pageSize = pageSize <= 0 ? 5 : pageSize;
        return PageHelper.startPage(pageNo, pageSize).doSelectPageInfo(() -> userMapper.selectUserAll());
    }
}
